import {
  homeworkInfo,
  saveQuestionList,
  checkQuestionList,
  correctHomework,
  completeHomework,
  homeworkUserInfo,
  examHeadInfo,
  getHistoryQuesion,
} from  '../../../service/api';
import { formatDate, myMessage } from '../../../utils/formatDataSource';

export default {
  namespace: 'question3',

  state: {
    lessonId: 0,
    homeworkInfo: {
      students: [],
    },
    xzData: { data: [] },
    tkData: { data: [] },
    jdData: { data: [] },
    homeworkUser: {},
    examHeadInfo: {},
    examHistory: {},
  },

  effects: {
    *getHistory({ payload }, { call, put }) {
      const response = yield call(getHistoryQuesion, payload);
      yield put({
        type: 'r_getHistory',
        payload: response,
      });
    },
    *homeworkInfo({ payload }, { call, put }) {
      const response = yield call(homeworkInfo, payload);
      yield put({
        type: 'r_homeworkInfo',
        payload: {
          lessonId: payload.lessonId,
          res: response,
        },
      });
    },
    *saveList({ payload }, { call, put }) {
      const response = yield call(saveQuestionList, payload);
      yield put({
        type: 'r_saveList',
        payload: response.data,
      });
    },
    *checkList1({ payload }, { call, put }) {
      const response = yield call(checkQuestionList, payload);
      yield put({
        type: 'r_checkList1',
        payload: response,
      });
    },
    *checkList2({ payload }, { call, put }) {
      const response = yield call(checkQuestionList, payload);
      yield put({
        type: 'r_checkList2',
        payload: response,
      });
    },
    *checkList3({ payload }, { call, put }) {
      const response = yield call(checkQuestionList, payload);
      yield put({
        type: 'r_checkList3',
        payload: response,
      });
    },
    *correct({ payload }, { call, put }) {
      const response = yield call(correctHomework, payload);
      yield put({
        type: 'r_correct',
        payload: response,
      });
    },
    *complete({ payload }, { call, put }) {
      const response = yield call(completeHomework, payload);
      yield put({
        type: 'r_complete',
        payload: response,
      });
    },
    *homeworkUserInfo({ payload }, { call, put }) {
      const response = yield call(homeworkUserInfo, payload);
      yield put({
        type: 'r_homeworkUserInfo',
        payload: response,
      });
    },
    *examInfo({ payload }, { call, put }) {
      const response = yield call(examHeadInfo, payload);
      yield put({
        type: 'r_examInfo',
        payload: response,
      });
    },
  },

  reducers: {
    r_getHistory(state, action) {
      if (action.payload) {
        const { data } = action.payload;
        state.examHistory = {
          ...data,
        };
      }
      return {
        ...state,
      };
    },
    r_homeworkInfo(state, action) {
      console.log(action.payload);
      state.lessonId = action.payload.lessonId;
      if (action.payload.res) {
        state.homeworkInfo = formatDate(action.payload.res.data);
      }
      return {
        ...state,
      };
    },
    r_saveList(state, action) {
      myMessage.success('保存成功', 2);
      return {
        ...state,
      };
    },
    r_checkList1(state, action) {
      if (action.payload) {
        state.xzData = action.payload.data;
      }
      return {
        ...state,
      };
    },
    r_checkList2(state, action) {
      if (action.payload) {
        state.tkData = action.payload.data;
      }
      return {
        ...state,
      };
    },
    r_checkList3(state, action) {
      if (action.payload) {
        state.jdData = action.payload.data;
      }
      return {
        ...state,
      };
    },
    r_correct(state, action) {
      return {
        ...state,
      };
    },
    r_complete(state, action) {
      myMessage.success('保存成功', 2);
      return {
        ...state,
      };
    },
    r_homeworkUserInfo(state, action) {
      if (action.payload) {
        const { data } = action.payload;
        state.homeworkUser = data;
      }
      return {
        ...state,
      };
    },
    r_examInfo(state, action) {
      if (action.payload) {
        const { data } = action.payload;
        state.examHeadInfo = data;
      }
      return {
        ...state,
      };
    },
  },
};
